import yaml
from pathlib import Path

# YAML 文件路径
storage_file = Path("account_data.yaml")


# 初始化存储
def load_storage():
    if not storage_file.exists():
        return {}
    with open(storage_file, "r", encoding="utf-8") as f:
        return yaml.safe_load(f) or {}


def save_storage(data):
    with open(storage_file, "w", encoding="utf-8") as f:
        yaml.dump(data, f, allow_unicode=True, default_flow_style=False)


def track_price_changes(new_data):
    # 加载存储数据
    storage = load_storage()

    added_accounts = []
    price_changed_accounts = []

    for account in new_data:
        account_data = {
            "price": [account[1]],
            "character": account[2],
            "weapon5_num": account[3],
        }

        account_id = account[0]

        if account_id not in storage:
            # 新账号，直接添加
            storage[account_id] = account_data
            added_accounts.append(account_id)
        else:
            # 已存在账号，检查价格是否发生变化
            if account[1] not in storage[account_id]["price"]:
                storage[account_id]["price"].append(account[1])
                price_changed_accounts.append(account_id)

    # 保存更新后的数据
    save_storage(storage)

    return added_accounts, price_changed_accounts


# 示例新抓取数据
new_data = [
    (
        "MRLGB6509",
        680,
        "68级，22黄，男主，11个五星角色：2命椿,1命维里奈,1命凌阳,今汐,折枝,守岸人,长离,安可,相里要,鉴心,卡卡罗",
        6,
    ),
    (
        "MNOYO9936",
        100,
        "68级，21黄，男主，10个五星角色：2命椿,1命维里奈,1命卡卡罗,守岸人,长离,今汐,吟霖,相里要,凌阳,安可",
        7,
    ),
    # 添加更多数据
]

# 调用函数
added, changed = track_price_changes(new_data)

print("新增账号编码：", added)
print("价格变化账号编码：", changed)
